package com.syqc.monitor.ui.carinfo.report

import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import autodispose2.autoDispose
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir
import com.syqc.comlib.base.LazyFragment
import com.syqc.comlib.callback.EmptyCallback
import com.syqc.comlib.rx.filterFast
import com.syqc.comlib.utils.startActivityTo
import com.syqc.entity.FeeInfo
import com.syqc.monitor.R
import com.syqc.monitor.databinding.FragmentReportFeeBinding
import com.syqc.monitor.databinding.IncludeDayChooseBinding
import com.syqc.monitor.databinding.IncludeFeeInfoBinding
import com.syqc.monitor.ui.cost.FeeDetailActivity
import com.syqc.utils.Keys
import com.syqc.utils.ToolsUtil
import com.syqc.videos.CarMonitorViewModel

/**
 *@Author Jasper
 *@Time   2020/10/21
 *@Desc   费用
 */
class CarReportFeeFragment :
    LazyFragment<FragmentReportFeeBinding>(FragmentReportFeeBinding::inflate) {

    private lateinit var loadService: LoadService<Any>
    private lateinit var mDayChooseVB: IncludeDayChooseBinding
    private lateinit var mFeeInfoVB: IncludeFeeInfoBinding

    private val mActivityVM by activityViewModels<CarMonitorViewModel>()

    private val mViewModel: ReportItemViewModel by viewModels()

    override fun initView(view: View) {
        mDayChooseVB = IncludeDayChooseBinding.bind(binding.root)
        mFeeInfoVB = IncludeFeeInfoBinding.bind(binding.root)
        mViewModel.dataType = 4
        loadService = LoadSir.getDefault().register(binding.flFeeContent)
    }

    private var fee: FeeInfo? = null
    override fun initData() {
        mActivityVM.carId.observe(this, Observer {
            if (it.isNullOrEmpty()) {
                loadService.showCallback(EmptyCallback::class.java)
            } else {
                mViewModel.carId = it
                if (!mViewModel.isHidden) mViewModel.handleTime()
            }
        })


        if (mViewModel.carId.isEmpty()) showToast(R.string.hint_toast_choose_car)
        loadService.showCallback(EmptyCallback::class.java)
        handleViewModel()
    }


    private fun handleViewModel() {
        val unitRmb = getString(R.string.unit_rmb)
        val unitRise = getString(R.string.unit_rise)
        mViewModel.feeDetailSub.hide().autoDispose(scopeProvider).subscribe {
            if (it.fees.isNotEmpty()) {
                loadService.showSuccess()
                fee = it.fees[0].apply {
                    mFeeInfoVB.lrTvFeeAddOil.setRightText("${ToolsUtil.formatTo2F(addOilFee)} $unitRmb")
                    mFeeInfoVB.lrTvFeeInsure.setRightText("${ToolsUtil.formatTo2F(insureFee)} $unitRmb")
                    mFeeInfoVB.lrTvFeeTraffic.setRightText("${ToolsUtil.formatTo2F(trafficFee)} $unitRmb")
                    mFeeInfoVB.lrTvFeeRepair.setRightText("${ToolsUtil.formatTo2F(repairFee)} $unitRmb")
                    mFeeInfoVB.lrTvFeeYear.setRightText("${ToolsUtil.formatTo2F(yearAdtFee)} $unitRmb")
                    mFeeInfoVB.lrTvFeeOther.setRightText("${ToolsUtil.formatTo2F(otherFee)} $unitRmb")
                    mFeeInfoVB.lrTvAddOil.setRightText("${ToolsUtil.formatTo2F(addOilCapacity)} $unitRise")
                    mFeeInfoVB.lrTvFeeTotal.setRightText("${ToolsUtil.formatTo2F(totalFee)} $unitRmb")
//                    mFeeInfoVB.lrTvFeeAddOil.setRightText("$addOilFee $unitRmb")
//                    mFeeInfoVB.lrTvFeeInsure.setRightText("$insureFee $unitRmb")
//                    mFeeInfoVB.lrTvFeeTraffic.setRightText("$trafficFee $unitRmb")
//                    mFeeInfoVB.lrTvFeeRepair.setRightText("$repairFee $unitRmb")
//                    mFeeInfoVB.lrTvFeeYear.setRightText("$yearAdtFee $unitRmb")
//                    mFeeInfoVB.lrTvFeeOther.setRightText("$otherFee $unitRmb")
//                    mFeeInfoVB.lrTvAddOil.setRightText("$addOilCapacity $unitRise")
//                    mFeeInfoVB.lrTvFeeTotal.setRightText("$totalFee $unitRmb")
                }

            } else {
                loadService.showCallback(EmptyCallback::class.java)
            }
        }
        mViewModel.toastObserver().autoDispose(scopeProvider).subscribe(this::toastObserver)
        mViewModel.feeInfoSub.hide().autoDispose(scopeProvider).subscribe {
            startActivityTo<FeeDetailActivity>(requireContext()) {
                putExtra(Keys.INTENT_DATA, it)
                putExtra(Keys.INTENT_OTHER, mViewModel.feeType)
            }
        }
        mViewModel.freeEmptySub.hide().autoDispose(scopeProvider).subscribe {
            if (it) {
                loadService.showCallback(EmptyCallback::class.java)
            } else {
                loadService.showSuccess()
            }
        }
    }

    override fun initEvent() {
        mDayChooseVB.rdbReportYesterday.setOnClickListener {
            mViewModel.timeType = 1
            mViewModel.handleTime()
        }
        mDayChooseVB.rdbReportToday.setOnClickListener {
            mViewModel.timeType = 2
            mViewModel.handleTime()
        }
        mDayChooseVB.rdbReportThree.setOnClickListener {
            mViewModel.timeType = 3
            mViewModel.handleTime()
        }
        mDayChooseVB.rdbReportWeek.setOnClickListener {
            mViewModel.timeType = 4
            mViewModel.handleTime()
        }

        mFeeInfoVB.lrTvFeeAddOil.filterFast {
            fee?.let {
                if (it.addOilFee <= 0) {
                    showToast(R.string.toast_no_fee)
                    return@filterFast
                }
                mViewModel.getFeeInfo("FeeAddOil")
            }
        }
        mFeeInfoVB.lrTvFeeInsure.filterFast {
            fee?.let {
                if (it.insureFee <= 0) {
                    showToast(R.string.toast_no_fee)
                    return@filterFast
                }
                mViewModel.getFeeInfo("FeeInsure")
            }

        }
        mFeeInfoVB.lrTvFeeTraffic.filterFast {
            fee?.let {
                if (it.trafficFee <= 0) {
                    showToast(R.string.toast_no_fee)
                    return@filterFast
                }
                mViewModel.getFeeInfo("FeeTraffic")
            }
        }
        mFeeInfoVB.lrTvFeeRepair.filterFast {
            fee?.let {
                if (it.repairFee <= 0) {
                    showToast(R.string.toast_no_fee)
                    return@filterFast
                }
                mViewModel.getFeeInfo("FeeRepair")
            }
        }
        mFeeInfoVB.lrTvFeeYear.filterFast {
            fee?.let {
                if (it.yearAdtFee <= 0) {
                    showToast(R.string.toast_no_fee)
                    return@filterFast
                }
                mViewModel.getFeeInfo("FeeYearVrf")
            }
        }
        mFeeInfoVB.lrTvFeeOther.filterFast {
            fee?.let {
                if (it.otherFee <= 0) {
                    showToast(R.string.toast_no_fee)
                    return@filterFast
                }
                mViewModel.getFeeInfo("FeeOther")
            }
        }
    }

    //是否隐藏状态变化监听
    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden) {
            mViewModel.isHidden = true
        } else {
            mViewModel.isHidden = false
            if (mViewModel.carId.isNotEmpty()) mViewModel.handleTime()

        }
    }
}